import axios from 'axios';
const baseUrl = 'http://localhost:8089';

// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};
// 通用公用方法 application/json
const reqPost = (method, url, params) => {
    return axios({
        method: method,
        url: baseUrl + url,
        headers: {
            'Content-Type': 'application/json' //如果写成contentType会报错
        },
        data: params,
        traditional: true
    }).then(res => res.data);
};

const reqGet = (method, url, params) => {
    return axios({
        method: method,
        url: baseUrl + url,
        headers: {
            'Content-Type': 'application/json',
        },
        params: params
    }).then(res => res.data);
};

const exportFile = (url, data) => {
    return axios({
        url: baseUrl + url,
        headers: {
            'Content-Type': 'application/json',
        },
        method: 'get',
        params: data,
        responseType: 'blob'
    }).then(res => res);
};

const loadFile = (method, url, params) => {
    return axios({
        method: method,
        url: baseUrl + url,
        headers: {
            Accept: 'application/octet-stream',
        },
        params: params,
        responseType: 'blob',
    }).then(res => res);
};

const uploadFile = (url, params) => {
    return axios({
        url: baseUrl + url,
        method: 'post',
        data: params
    }).then(res => res)
}

export {
    loginreq,
    reqPost,
    reqGet,
    exportFile,
    loadFile,
    uploadFile
}